<template>
	<view>
		<view @click="setAddress">
			<p v-if="currentAddress[2] != null" style="color: #000000;">
				{{address}}
			</p>
			<p v-else>
				省/市/区
			</p>
		</view>
		<uni-popup ref="popup" type="bottom">
			<view class="select-border back-white">
				<view class="header">
					<view class="title">
						选择地区
					</view>
					<view class="cancel-icon" @tap="$refs.popup.close()">
						X
					</view>
				</view>
				<view class="select-box">
					<view class="score-item item1">
						<scroll-view class="scroll-content" :scroll-y="true" style="height: calc(60vh);">
							<view v-for="(item, index) in addressInfo" :key="index">
								<view @click="province(index)" :class="index == currentAddress[0] ? 'text-red' : ''">
									{{item.name}}
								</view>
							</view>
						</scroll-view>
					</view>
					<view class="score-item item1">
						<scroll-view class="scroll-content" :scroll-y="true" style="height: calc(60vh);">
							<view v-for="(item, index) in city" :key="index">
								<view @click="cityCheck(index)" :class="index == currentAddress[1] ? 'text-red' : ''">
									{{item.name}}
								</view>
							</view>
						</scroll-view>
					</view>
					<view class="score-item item1">
						<scroll-view class="scroll-content" :scroll-y="true" style="height: calc(60vh);">
							<view v-for="(item, index) in town" :key="index">
								<view @click="doneCheck(index)" :class="index == currentAddress[2] ? 'text-red' : ''">
									{{item.name}}
								</view>
							</view>
						</scroll-view>
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import uniPopup from '@/components/uni-popup/uni-popup.vue'
	export default {
		components: {
			uniPopup
		},
		props: {
			addressInfo: {
				type: Array,
				default: []
			}
		},
		data() {
			return {
				town: [],
				city: [],
				currentAddress: [],
				address: "",
			};
		},
		methods: {
			doneCheck(index) {
				this.$set(this.currentAddress, 2, index);
				this.address = (this.addressInfo[this.currentAddress[0]]).name + ' ' +
				(this.city[this.currentAddress[1]]).name + ' ' +
				(this.town[this.currentAddress[2]]).name;
				this.$refs.popup.close();
				this.$emit("getAddressValue", 
				(this.addressInfo[this.currentAddress[0]]).name,
				(this.city[this.currentAddress[1]]).name,(this.town[this.currentAddress[2]]).name);
			},
			cityCheck(index) {
				this.$set(this.currentAddress, 1, index);
				this.town = this.addressInfo[this.currentAddress[0]].sub[this.currentAddress[1]].sub;
			},
			province(index) {
				this.$set(this.currentAddress, 0, index);
				this.city = this.addressInfo[index].sub;
				this.town = [];
			},
			setAddress() {
				this.$refs.popup.open()
			},
		}
	}
</script>

<style>
	@import url("/static/css/common.css");

	p {
		/* height: calc(10vh); */
		width: calc(70vw);
		line-height: calc(10vh);
		color: #888888;
	}

	.select-border {
		height: calc(60vh);
		padding: calc(2vw);
	}

	.header {
		display: flex;
		justify-content: space-between;
	}

	.scroll-content {
		white-space: nowrap;
		overflow: hidden;
		width: calc(33.33vw);
		scrollbar-width: none;
	}

	scroll-view view {
		margin: calc(1vw);
	}

	.select-box {
		display: flex;
		text-align: center;
	}

	.text-red {
		color: red;
	}
</style>
